import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController, LoadingController  } from 'ionic-angular';
import { AddBankTwoPage } from '../add-bank-two/add-bank-two';
import * as bin from 'bankcardinfo'
/**
 * Generated class for the AddBankPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-add-bank',
  templateUrl: 'add-bank.html',
})
export class AddBankPage {

    constructor(public navCtrl: NavController, public navParams: NavParams, public toastCtrl: ToastController, public loadingCtrl: LoadingController) {
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad AddBankPage');
    }

    next(man, cardId){

        if (man.length >= 2 && cardId.length >= 16){
            //6227003320240034988
            console.log(cardId)
            const loader = this.loadingCtrl.create({
                content: "请稍后",
                duration: 1000
            });
            bin.getBankBin(cardId).then((data) => {
                let bankName = data.bankName
                loader.present();                
                this.navCtrl.pop();
                this.navCtrl.push(AddBankTwoPage, { man: man, cardId: cardId, bankName: bankName});
            }).catch((err) => {
                console.log(err)
                this.presentToast(err);
            })
            
            
        }else{
            this.presentToast('请填写相关信息,信息错误将造成损失');
        }
	    
    }

    presentToast(text) {
        let toast = this.toastCtrl.create({
            message: text,
            duration: 3000
        });
        toast.present();
    }
}
